package com.cal.utms.action.maintenance;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.dao.maintenance.FundManagerDaoImpl;
import com.cal.utms.dto.maintenance.FMContactPersonDto;
import com.cal.utms.dto.maintenance.FundManagerDto;
import com.cal.utms.entity.FundManager;
import com.cal.utms.model.maintenance.FundManContactPersonModel;
import com.cal.utms.model.maintenance.FundManagerModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class FundManagerAction extends ActionSupport implements ModelDriven<FundManagerDto> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	FundManagerDaoImpl fundManagerDao = new FundManagerDaoImpl();
	private FundManagerDto fundManagerBean = new FundManagerDto();
	private String contactPersons;
	private int fund_manager_id;

	public String addFundManager() {
		contactPersons = "{ \"contacts\" : ".concat(contactPersons) + " }";
		Object object = fundManagerDao.addFundManager(FundManagerModel.createFundManagerModel(fundManagerBean));

		if (object instanceof Integer) {
			fundManagerBean.setId((Integer) object);
			List<FMContactPersonDto> contactPersonBeans = new ArrayList<FMContactPersonDto>();
			try {
				JSONObject jsonObject = new JSONObject(contactPersons);
				JSONArray jArray = jsonObject.getJSONArray("contacts");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					FMContactPersonDto bean = new FMContactPersonDto(jObj.getString("cpname"), jObj.getString("cpDesignation"), jObj.getString("cpPhone"), jObj.getString("cpFax"),
							jObj.getString("cpEmail"), FundManagerModel.createFundManagerModelWithId(fundManagerBean));
					contactPersonBeans.add(bean);
				}
				fundManagerDao.addFundManagerContactPersons(FundManContactPersonModel.createModleList(contactPersonBeans));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			fundManagerBean.setSuccess(true);
		}
		return Action.SUCCESS;
	}

	public String updateFundManagerObj() {
		contactPersons = "{ \"contacts\" : ".concat(contactPersons) + " }";
		fundManagerBean.setId(fund_manager_id);
		FundManager fundManager = FundManagerModel.createFundManagerModelWithId(fundManagerBean);
		fundManagerDao.updateFundManager(fundManager);
		// if (object instanceof Integer) {
		List<FMContactPersonDto> contactPersonBeans = new ArrayList<FMContactPersonDto>();
		try {
			JSONObject jsonObject = new JSONObject(contactPersons);
			JSONArray jArray = jsonObject.getJSONArray("contacts");
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jObj = jArray.getJSONObject(i);
				FMContactPersonDto bean = new FMContactPersonDto(jObj.getString("cpname"), jObj.getString("cpDesignation"), jObj.getString("cpPhone"), jObj.getString("cpFax"),
						jObj.getString("cpEmail"), fundManager);
				contactPersonBeans.add(bean);
			}
			fundManagerDao.addUpdateFundManagerContactPersons(FundManContactPersonModel.createModleList(contactPersonBeans), fund_manager_id);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		fundManagerBean.setSuccess(true);
		// }
		return Action.SUCCESS;
	}

	public String getAllFundMangers() {
		fundManagerBean.setSuccess(true);
		fundManagerBean.setFundManagerBeans(FundManagerModel.createFundManagerBealList(fundManagerDao.getFundManagerList()));
		return Action.SUCCESS;
	}

	@Override
	public FundManagerDto getModel() {
		return fundManagerBean;
	}

	public FundManagerDto getFundManagerBean() {
		return fundManagerBean;
	}

	public void setFundManagerBean(FundManagerDto fundManagerBean) {
		this.fundManagerBean = fundManagerBean;
	}

	public String getContactPersons() {
		return contactPersons;
	}

	public void setContactPersons(String contactPersons) {
		this.contactPersons = contactPersons;
	}

	public int getFund_manager_id() {
		return fund_manager_id;
	}

	public void setFund_manager_id(int fund_manager_id) {
		this.fund_manager_id = fund_manager_id;
	}
}
